package com.ys.timer;

import com.ys.service.business.TpAnalysisDailyTargetService;
import com.ys.service.business.TpAnalysisDailyTargetSetService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Controller;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;


/**
 * 运营日报表
 */
@Controller
public class TimerTpAnalysisDailyTargetMonthAndDaily {
	private static final Logger logger = LoggerFactory.getLogger(TimerTpAnalysisDailyTargetMonthAndDaily.class);

	@Autowired
	private TpAnalysisDailyTargetSetService tpAnalysisDailyTargetSetService;

	@Autowired
	private TpAnalysisDailyTargetService tpAnalysisDailyTargetService;


	//新增这个月的数据
	@Scheduled(cron = "0 0 2 2 * ?")//每月2号2点触发任务
	//@Scheduled(cron = "5 * * * * * ")//开发测试用
	public void addTpAnalysisDailyTargetSetByShop() {
		try {
			logger.info("***********每月定时新增运营日报表日目标设置，生成日均目标***开始***********");
			logger.info("修改数据，填入上月的增长率");
			//获取这个月时间
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
			Calendar calendar = Calendar.getInstance();
			String thisMonth = sdf.format(calendar.getTime());//这个月
			calendar.add(Calendar.MONTH, -1);
			String oldMonth = sdf.format(calendar.getTime());//上个月
			tpAnalysisDailyTargetSetService.editOldMonthData(oldMonth);
			logger.info("新增本月目标数据");
            tpAnalysisDailyTargetSetService.addThisMonth(thisMonth);
			logger.info("***********每月定时新增运营日报表日目标设置，生成日均目标***完成***********");
		}catch (Exception e){
			logger.error("定时新增运营日报表，生成日均目标出错：",e.getMessage(),e);
		}
	}

	//每日更新前一日统计数据
	//@Scheduled(cron = "0 0 10 * * ?")//每天9点30执行,刷新14天数据
	public void addTpAnalysisDailyTargetByShop() {
		try {
			logger.info("***********每日9点30定时新增运营日报表，统计日增长率***开始***********");
			for(int i=1;i<=14;i++){
				//获取昨天时间日期
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
				Calendar calendar = Calendar.getInstance();
				calendar.add(Calendar.DAY_OF_MONTH, -i);
				String agoDay = sdf.format(calendar.getTime());
				logger.info("*************刷新运营日报表:"+agoDay+" 记录*************");
				tpAnalysisDailyTargetService.addByDay(agoDay);
			}
			logger.info("***********每日定时新增运营日报表，统计日增长率***完成***********");
		}catch (Exception e){
			logger.error("每日定时新增运营日报表，统计日增长率出错：",e.getMessage(),e);
		}
	}
	//每日更新前一日统计数据
	//@Scheduled(cron = "0 30 16 * * ? ")//每天16点30执行,刷新昨天的数据
	public void addTpAnalysisDailyTargetByShopOneDay() {
		try {
			logger.info("***********每日16点30定时新增运营日报表，统计日增长率***开始***********");
			//获取昨天时间日期
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			Calendar calendar = Calendar.getInstance();
			calendar.add(Calendar.DAY_OF_MONTH, -1);
			String agoDay = sdf.format(calendar.getTime());
			logger.info("*************刷新运营日报表:"+agoDay+" 记录*************");
			tpAnalysisDailyTargetService.addByDay(agoDay);
			logger.info("***********每日定时新增运营日报表，统计日增长率***完成***********");
		}catch (Exception e){
			logger.error("每日定时新增运营日报表，统计日增长率出错：",e.getMessage(),e);
		}
	}



}
